class WsBaseClass
{
  constructor(url,binaryType){
    this.url = url;
    this.binaryType = binaryType ? binaryType : null;
    this.ws = null;
    this.init();
  }

  init(){
    this.ws = new WebSocket(this.url);
    if(this.ws.binaryType)
    {
      this.ws.binaryType = 'arraybuffer';
    }
    // this.ws.onopen = this.onOpen.bind(this);
    // this.ws.onmessage = this.onMessage.bind(this);
    // this.ws.onclose = this.onClose.bind(this);
    this.ws.addEventListener('open',  this.onOpen.bind(this));
    this.ws.addEventListener('message',  this.onMessage.bind(this));
    this.ws.addEventListener('error',  this.onError.bind(this));
    this.ws.addEventListener('close',  this.onClose.bind(this));
  }
  onOpen(event){}
  onMessage(event){}
  onClose(event){
    this.ws.removeEventListener('open',  this.onOpen.bind(this));
    this.ws.removeEventListener('message',  this.onMessage.bind(this));
    this.ws.removeEventListener('error',  this.onError.bind(this));
    this.ws.removeEventListener('close',  this.onClose.bind(this));
    this.ws = null;
  }
  onError(event){console.error(event)}
}


export default WsBaseClass;